package view;

import control.KepalaDapurKontrol;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Timer;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.Menu;
import model.Pembeli;
import model.Pesan;
import tabelmodel.PesanTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Asus
 */
public class frameDaftarPesanan extends javax.swing.JFrame {

    /**
     * Creates new form frameDaftarPesanan
     */
    public frameDaftarPesanan() {
        initComponents();
        tampilPesanan();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDaftarPesanan = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelDaftarPesanan = new javax.swing.JTable();
        tombolKeluarDaftarPesanan = new javax.swing.JButton();
        tombolReload = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelDaftarPesanan.setText("Daftar Pesanan");

        tabelDaftarPesanan.setAutoCreateRowSorter(true);
        tabelDaftarPesanan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Id Pesanan", "Waktu Pesan", "No.Meja", "Nama Menu", "Jumlah Pesanan", "Status"
            }
        ));
        tabelDaftarPesanan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelDaftarPesananMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelDaftarPesanan);

        tombolKeluarDaftarPesanan.setText("Keluar");
        tombolKeluarDaftarPesanan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolKeluarDaftarPesananActionPerformed(evt);
            }
        });

        tombolReload.setText("Muat Ulang");
        tombolReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tombolReloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(labelDaftarPesanan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tombolKeluarDaftarPesanan)
                        .addGap(34, 34, 34))))
            .addGroup(layout.createSequentialGroup()
                .addGap(313, 313, 313)
                .addComponent(tombolReload)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDaftarPesanan)
                    .addComponent(tombolKeluarDaftarPesanan))
                .addGap(39, 39, 39)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(tombolReload)
                .addGap(60, 60, 60))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tombolKeluarDaftarPesananActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolKeluarDaftarPesananActionPerformed
        // TODO add your handling code here:
        this.dispose();
        HomePageDesktop home = new HomePageDesktop();
        home.setVisible(true);
    }//GEN-LAST:event_tombolKeluarDaftarPesananActionPerformed

    private void tabelDaftarPesananMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelDaftarPesananMouseClicked
        // TODO add your handling code here:
        Pesan pesanan = new Pesan();
        int baris = tabelDaftarPesanan.getSelectedRow();
        String id = tabelDaftarPesanan.getValueAt(baris, 0).toString();
        String waktu = tabelDaftarPesanan.getValueAt(baris, 1).toString();
        String noMeja = tabelDaftarPesanan.getValueAt(baris, 2).toString();
        String menu = tabelDaftarPesanan.getValueAt(baris, 3).toString();
        String jumlah = tabelDaftarPesanan.getValueAt(baris, 4).toString();
        String status = tabelDaftarPesanan.getValueAt(baris, 5).toString();
        pesanan.setIdPesanan(id);
        pesanan.setStatus(status);
        if (pesanan.getStatus().equals("Menunggu")) {
            int opsi = JOptionPane.showConfirmDialog(null, "Yakin Update Status ? ", "Konfirmasi", JOptionPane.YES_NO_OPTION);
            if (opsi == JOptionPane.YES_OPTION) {
                try {
                    KepalaDapurKontrol.getKoneksiKepalaDapur().updateStatus(pesanan);
                } catch (SQLException ex) {
                    Logger.getLogger(frameAdmin.class.getName()).log(Level.SEVERE, null, ex);
                }
                tampilPesanan();
            } else {
                tampilPesanan();
            }
        } else if (pesanan.getStatus().equals("Selesai")) {
            JOptionPane.showMessageDialog(null, "Pesanan Sudah Diselesaikan");
            tampilPesanan();
        }
    }//GEN-LAST:event_tabelDaftarPesananMouseClicked

    private void tombolReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolReloadActionPerformed
        // TODO add your handling code here:
        tampilPesanan();
    }//GEN-LAST:event_tombolReloadActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frameDaftarPesanan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frameDaftarPesanan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frameDaftarPesanan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frameDaftarPesanan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frameDaftarPesanan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDaftarPesanan;
    private javax.swing.JTable tabelDaftarPesanan;
    private javax.swing.JButton tombolKeluarDaftarPesanan;
    private javax.swing.JButton tombolReload;
    // End of variables declaration//GEN-END:variables

    private void tampilPesanan() {
        try {
            List<Pesan> daftarPesanan = KepalaDapurKontrol.getKoneksiKepalaDapur().tampilDaftarPesanan();
            PesanTableModel model = new PesanTableModel(daftarPesanan);
            tabelDaftarPesanan.setModel(model);
        } catch (SQLException ex) {
            Logger.getLogger(frameDaftarPesanan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
